/**
 * 
 */
package com.manthano.applogin.management.controller;

import com.manthano.applogin.management.bo.AppUser;
import com.manthano.applogin.management.data.UserType;
import com.manthano.applogin.management.exception.UserException;
import com.manthano.applogin.management.service.AppUserService;
import com.manthano.applogin.management.util.PasswordGenerationUtil;
import com.manthano.util.context.Context;
import com.manthano.util.web.service.ServiceController;
import com.manthano.util.web.service.bo.ServiceRequest;
import com.manthano.util.web.service.bo.ServiceResponse;
import com.manthano.util.web.service.exception.ServiceParameterException;

/**
 * @author Dell
 */
public class AppUserController implements ServiceController
{
	/**
	 * @param serviceRequest
	 * @param serviceResponse
	 * @throws ServiceParameterException
	 * @throws UserException
	 */
	public void addUpdateUser(ServiceRequest serviceRequest, ServiceResponse serviceResponse)
			throws ServiceParameterException, UserException
	{
		AppUserService appUserService = (AppUserService) Context.getBean("appUserService");
		String userId = serviceRequest.getString("userId", 40);

		AppUser appUser = null;
		if (userId == null || userId.isEmpty())
		{
			appUser = new AppUser();
		}
		else
		{
			appUser = appUserService.getUser(userId);
		}
		appUser.setUserName(serviceRequest.getString("firstName", 100));
		appUser.setDesignation(serviceRequest.getString("designation", 100));
		appUser.setEmailAddress(serviceRequest.getMandatoryString("emailAddress", 100));
		appUser.setUserType(UserType.TENANT_USER);
		appUser.setFirstName(serviceRequest.getMandatoryString("firstName", 100));
		appUser.setLastName(serviceRequest.getString("lastName", 100));
		appUser.setInitials(serviceRequest.getString("initials", 10));
		appUser.setAcceptTermsConditions(serviceRequest.getBoolean("termsConditions", false));
		appUser.setPasswordHash(PasswordGenerationUtil.generateSpringPassword(serviceRequest
				.getMandatoryString("password", 100)));
		serviceResponse.setResult(appUserService.addUser(appUser));
	}

	/**
	 * @param serviceRequest
	 * @param serviceResponse
	 * @throws ServiceParameterException
	 * @throws UserException
	 */
	public void activateUser(ServiceRequest serviceRequest, ServiceResponse serviceResponse)
			throws ServiceParameterException, UserException
	{
		AppUserService appUserService = (AppUserService) Context.getBean("appUserService");
		serviceResponse.setResult(appUserService.activateUser(serviceRequest.getMandatoryString("userId", 40)));
	}

	/**
	 * @param serviceRequest
	 * @param serviceResponse
	 * @throws ServiceParameterException
	 * @throws UserException
	 */
	public void deactivateUser(ServiceRequest serviceRequest, ServiceResponse serviceResponse)
			throws ServiceParameterException, UserException
	{
		AppUserService appUserService = (AppUserService) Context.getBean("appUserService");
		serviceResponse.setResult(appUserService.deactivateUser(serviceRequest.getMandatoryString("userId", 40)));
	}

	/**
	 * @param serviceRequest
	 * @param serviceResponse
	 * @throws ServiceParameterException
	 * @throws UserException
	 */
	public void removeUser(ServiceRequest serviceRequest, ServiceResponse serviceResponse)
			throws ServiceParameterException, UserException
	{
		AppUserService appUserService = (AppUserService) Context.getBean("appUserService");
		serviceResponse.setResult(appUserService.removeUser(serviceRequest.getMandatoryString("userId", 40)));
	}

	/**
	 * @param serviceRequest
	 * @param serviceResponse
	 * @throws ServiceParameterException
	 * @throws UserException
	 */
	public void getUser(ServiceRequest serviceRequest, ServiceResponse serviceResponse)
			throws ServiceParameterException, UserException
	{
		AppUserService appUserService = (AppUserService) Context.getBean("appUserService");
		serviceResponse.setResult(appUserService.getUser(serviceRequest.getMandatoryString("userId", 40)));
	}

	/**
	 * @param serviceRequest
	 * @param serviceResponse
	 * @throws ServiceParameterException
	 * @throws UserException
	 */
	public void getUserByUserName(ServiceRequest serviceRequest, ServiceResponse serviceResponse)
			throws ServiceParameterException, UserException
	{
		AppUserService appUserService = (AppUserService) Context.getBean("appUserService");
		serviceResponse.setResult(appUserService.getUserByUserName(serviceRequest.getMandatoryString("userName", 40)));
	}
}
